﻿using Sustainalytics.ClientDW.Synchronization.RiskRatingsBenchmarks;
using Sustainalytics.Entities.RiskRatings;
using System;
using System.Linq;
using Xunit;
using Xunit.Abstractions;

namespace Sustainalytics.Benchmarkings.Tests
{

    [Trait("Category", "Unit")]
    public class MarketCapTests : IClassFixture<ComparativeGroupFixture>
    {
        private readonly ComparativeGroupFixture _comparativeGroupFixture;
        private readonly ComparativeGroup<RiskRatingResearchProfile, Guid, int> _comparativeGroup;
        private MarketCapBenchmark<RiskRatingResearchProfile, Guid, int> _sut;
        private readonly ITestOutputHelper _ouput;

        public MarketCapTests(ComparativeGroupFixture comparativeGroupFixture, ITestOutputHelper ouput)
        {
            _comparativeGroupFixture = comparativeGroupFixture;
            _comparativeGroup = ComparativeGroup<RiskRatingResearchProfile, Guid, int>.Create(_comparativeGroupFixture.GetCorporateData(), x => x.Id, x => x.CompanyName, x => x.SubPeerGroupId, new SubPeerGroupComparer(), x => x.ManagementRisk.UnmanagedRisk.Value, new DoubleComparer());
            _sut = new MarketCapBenchmark<RiskRatingResearchProfile, Guid, int>(x=>x.MarketCapitalization, _comparativeGroup, new CustomComparer());
            _ouput = ouput;
        }

       

        [Fact]
        public void GetCompanyPeers_should_return_at_most_5_peers()
        {
            var companyPeers = _sut.GetIndustryPeers(_comparativeGroupFixture.GetCompanyIdByIndex(0));

            Assert.True(companyPeers.PeerList.Count <= 5);
        }


        [Fact]
        public void GetCompanyPeers_should_contain_reference_company()
        {
            var company = _comparativeGroupFixture.GetCompanyIdByIndex(0);
            var companyPeers = _sut.GetIndustryPeers(company);

            Assert.True(companyPeers.PeerList.FirstOrDefault(x => x.Id == company) != null);
        }


        [Fact]
        public void GetCompanyPeers_should_contain_MarketCap_companies_in_same_comparative_group_as_reference_company()
        {
            var company = _comparativeGroupFixture.GetCompanyByIndex(0);
            var referenceCompanyGroupId = _comparativeGroup.GetGroupIndex(company.Id);
            var companyPeers = _sut.GetIndustryPeers(company.Id);
            Assert.True(companyPeers.PeerList.All(x => _comparativeGroup.GetGroupIndex(x.Id)==referenceCompanyGroupId));
        }


    }
}